local Color = require('libs/colorlibrary/colorlibrary')
local Core = require('src/Core')
local Graphics = require('src/graphics')
local Log = require('src/Log')
local Picture = require('src/Widgets/Picture')
local State = require('src/State')
local Widget = require('src/Widgets/Widget')

--local fadeState = {}
local fadeState = State:derive('Screen')
--local fadeState = Class:derive('Screen')

-- ----------

-- state --
function fadeState:new(name)
--print('SS: NEW !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
    Log:info('FadeState. Creating new screen: '..name)
--    fadeState.super.new(self)
    self.name = name
    -- set default values
    self.duration = 2.0
--    self.following = 'MainMenu'
--    self.skip = 'MainMenu'
    self.fadein = true
    self.fadeout = false
    -- screen dimensions
    local sw, sh = love.window.getMode()

    self.timeStart = love.timer.getTime()
    self.status = 'running'
    self.value = 0
print('Загружать fadeState:load и там настраивать value в зависимости от типа fadein / fadeout')
end

function fadeState:load()
print('FADE LOAD')
    self.timeStart = love.timer.getTime()
    -- events
--    ENGINE.EVENTS:hook('key_pressed', self.skipScreen)
end

function fadeState:update(dt)
--    self.cover:update(dt)
    local t = love.timer.getTime()
    self.value = 1 - (t - self.timeStart)/self.duration
    if (t - self.timeStart) > self.duration then
        ENGINE.SM:Pop(self)
    end
end

function fadeState:draw()
    -- draw background
--    love.graphics.setColor(0.5, 0.5, 0.5, 0.5)
    local v = self.value
    love.graphics.setColor(0, 0, 0, v)
    love.graphics.rectangle('fill', 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
--    self.bg:draw()
--    self.label:draw()
--    Graphics:ShowFPS()
end


function fadeState:exit()
    -- restore colors
    love.graphics.setColor(1, 1, 1, 1)
--    ENGINE.EVENTS:unhook('key_pressed', self.skipScreen)
--    ENGINE.EVENTS:remove('key_pressed')
end


--function fadeState:skipScreen()
--    -- get skip to
--    local here = Core.here()
--    local loc = Core.getReference(here)
--    Core.walk(loc.skip)
--end

--function love.mousemoved(x, y, dx, dy, istouch)
--    menuSystem.mousemoved(x, y)
--end

-- ----------

return fadeState
